<template>
    <div>
        <div>
            <el-col :span="6">
                <div class="grid-content bg-purple"></div>
                <el-button type="primary" @click="onhandAdd">添加洗衣方式</el-button>
            </el-col>
        </div>
        <el-table :data="tableData" stripe style="width: 100%">
            <el-table-column prop="washKindName" label="洗衣方式" width="180">
            </el-table-column>
            <el-table-column prop="washTime" label="此方式预计需要时间" width="180">
            </el-table-column>
            <el-table-column label="操作" prop="washKindId">
                <template v-slot="scope">
                    <el-button type="primary" @click="handleEdit(scope.row.washKindId)">编辑
                    </el-button>
                    <el-popconfirm confirm-button-text='确认' cancel-button-text='取消' icon="el-icon-info" icon-color="red"
                                   title="确认删除此洗衣方式吗？" @confirm="del(scope.row.washKindId)">
                        <el-button type="danger" slot="reference">删除</el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <div style="margin-top: 10px;">
            <el-pagination background :current-page="params.pageNum" :page-size="params.pageSize"
                           layout="prev, pager, next" :total="total" @current-change="handleCurrentChange">
            </el-pagination>
        </div>
        <!-- 新增编辑弹框 -->
        <el-dialog :title="title" :visible.sync="dialogVisible" width="40%" :before-close="handleClose">
            <el-form :inline="true" :model="washkindform" status-icon :rules="rules" ref="washkindform" style="width: 80%;"
                     label-width="200px">
                <el-form-item label="洗衣方式" prop="washKindName">
                    <el-input v-model="washkindform.washKindName" placeholder="请输入洗衣方式"></el-input>
                </el-form-item>
                <el-form-item label="此方式预计需要时间" prop="washTime">
                    <el-input v-model="washkindform.washTime" placeholder="请输入此方式预计需要的时间" oninput="value=value.replace(/[^0-9]/g,'')"></el-input>
                </el-form-item>

            </el-form>
            <div style="text-align-last: center;width: 60%;">
                <el-button @click="closedialog('washkindform')" type="danger">取 消</el-button>
                <el-button @click="resetForm('washkindform')" type="warning">重置</el-button>
                <el-button style="margin-right: 30px;" type="primary" icon="el-icon-upload2 "
                           @click="dialogVisibleConfirm('washkindform')" size="medium">添加</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import request from '@/request'

    export default {
        name: 'WashKind',
        data() {
            return {
                tableData: [],
                total: 0,
                allid: "",
                title: "",
                dialogVisible: false,
                params: {
                    pageNum: 1,
                    pageSize: 8
                },
                washkindform: {
                    washKindName: '',
                    washTime: ''
                },

                rules: {
                    washKindName: [{
                        required: true,
                        message: '洗衣方式不能为空',
                        trigger: 'blur'
                    }, ],
                    washTime: [{
                        required: true,
                        message: '所需时间不能为空',
                        trigger: 'blur'
                    }, ],

                }
            }
        },
        mounted() {
            this.load()
        },
        methods: {
            add(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        request.post('/washkind/add', this.washkindform).then(res => {
                            if (res.code === '200') {
                                this.$notify.success('新增成功')
                                this.resetForm(formName) //清空表格数据
                                this.dialogVisible = false; //关闭弹框
                                this.load(); //刷新列表
                            } else {
                                this.$notify.error(res.msg)
                            }
                        })
                    }
                });
            },
            update(formName){
                request.put('/washkind/update',this.washkindform).then(res=>{
                    console.log(res)
                    if(res.code==='200'){
                        this.$notify.success('修改成功')
                        this.resetForm(formName) //清空表格数据
                        this.dialogVisible = false; //关闭弹框
                        this.load(); //刷新列表
                    }else{
                        this.$notify.error(res.msg)
                    }
                })
            },
            handleClose(done) {
                this.dialogVisible = false;
            },
            onhandAdd() {
                this.title='添加洗衣方式';
                this.resetForm('washkindform');
                this.dialogVisible = true;
            },
            handleEdit(washKindId) {
                this.title='修改洗衣方式'
                this.dialogVisible = true; //显示弹框
                request.get("/washkind/"+washKindId).then(res=>{
                    this.washkindform=res.data
                })//row当前行数据，把当前行的数据赋值给 表单
                this.allid = washKindId; //把id存全局
            },

            //清空表单
            resetForm(formName) {
                this.$nextTick(()=>{
                    this.$refs[formName].resetFields();
                })
            },
            //页面加载
            load() {
                request.get('/washkind/page', {
                    params: this.params
                }).then(res => {
                    console.log(res)
                    if (res.code === "200") {
                        this.total = res.data.total;
                        this.tableData = res.data.list;
                        console.log(this.tableData)
                    }
                })
            },


            //核心判断哪一个按钮调用表单组件
            dialogVisibleConfirm(formName) {
                // 新增接口
                if (!this.allid) {
                    this.add(formName)

                } else {
                    // 修改接口
                    this.update(formName);
                }
            },

            closedialog(formName){
                this.dialogVisible = false;
                this.resetForm(formName) //清空表格数据
            },
            del(washKindId) {
                request.delete('/washkind/delete' + washKindId).then(res => {
                    console.log(washKindId)
                    if (res.code === '200') {
                        this.$notify.success('删除成功')
                        this.load();
                    } else {
                        this.$notify.error(res.msg)
                    }
                })

            },
            handleCurrentChange(pageNum) {
                this.params.pageNum = pageNum;
                this.load()
            }
        }
    }
</script>
